package com.example.exercise.sys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.sys.vo.SysMenuVO;
import com.example.exercise.sys.dao.entity.SysMenuEntity;
import com.example.exercise.sys.dao.SysMenuDAO;

/**
* 菜单表
*/
@Service
public class SysMenuService {

    @Autowired
    private SysMenuDAO sysMenuDAO;

    public List<SysMenuVO> findByWhere(Page<?> page, SysMenuVO sysMenuVO){
        List<SysMenuVO> list = new ArrayList<>();
        List<SysMenuEntity> list0 = sysMenuDAO.findByWhere(page, sysMenuVO);
        for (SysMenuEntity sysMenuEntity : list0) {
            list.add(toSysMenuVO(sysMenuEntity));
        }
        return list;
    }

    public SysMenuVO findByKey(SysMenuVO sysMenuVO){
        SysMenuEntity sysMenuEntity = sysMenuDAO.findByKey(sysMenuVO);
        return toSysMenuVO(sysMenuEntity);
    }

    public int insert(SysMenuVO sysMenuVO){
        return sysMenuDAO.insert(toSysMenuEntity(sysMenuVO));
    }

    public int updateByKey(SysMenuVO sysMenuVO){
        return sysMenuDAO.updateByKey(toSysMenuEntity(sysMenuVO));
    }

    public int deleteByKey(SysMenuVO sysMenuVO){
        return sysMenuDAO.deleteByKey(sysMenuVO);
    }

    private SysMenuVO toSysMenuVO(SysMenuEntity sysMenuEntity) {
        SysMenuVO sysMenuVO = new SysMenuVO();
                sysMenuVO.setId(String.valueOf(sysMenuEntity.getId()));
        sysMenuVO.setMenuid(sysMenuEntity.getMenuid());
        sysMenuVO.setMenuparentid(sysMenuEntity.getMenuparentid());
        sysMenuVO.setMenuname(sysMenuEntity.getMenuname());
        sysMenuVO.setMenuno(String.valueOf(sysMenuEntity.getMenuno()));
        sysMenuVO.setMenuurl(sysMenuEntity.getMenuurl());
        sysMenuVO.setMenuimgurl(sysMenuEntity.getMenuimgurl());

        return sysMenuVO;
    }

    private SysMenuEntity toSysMenuEntity(SysMenuVO sysMenuVO) {
        SysMenuEntity sysMenuEntity = new SysMenuEntity();
                String id = sysMenuVO.getId();
        if(id != null) {
            sysMenuEntity.setId(Integer.parseInt(sysMenuVO.getId()));
        }
        sysMenuEntity.setMenuid(sysMenuVO.getMenuid());
        sysMenuEntity.setMenuparentid(sysMenuVO.getMenuparentid());
        sysMenuEntity.setMenuname(sysMenuVO.getMenuname());
        String menuno = sysMenuVO.getMenuno();
        if(menuno != null) {
            sysMenuEntity.setMenuno(Integer.parseInt(sysMenuVO.getMenuno()));
        }
        sysMenuEntity.setMenuurl(sysMenuVO.getMenuurl());
        sysMenuEntity.setMenuimgurl(sysMenuVO.getMenuimgurl());

        return sysMenuEntity;
    }

}